﻿#include "client_qt.h"
#include <QHostAddress>

ClientQt::ClientQt() {
	
}

ClientQt::~ClientQt() {
	qDebug() << "ClientQt::~ClientQt()";
}

void ClientQt::Start(unsigned short port, const char* message) {
	QObject::connect(&socket_, &QTcpSocket::readyRead, [this]() {OnReadyRead();});
	QObject::connect(&socket_, &QTcpSocket::connected, [this, message]() {
		socket_.write(message);
		socket_.flush();
	});
	socket_.connectToHost(QHostAddress::LocalHost, static_cast<quint16>(port));
	socket_.moveToThread(&thread_);
	thread_.start();
}

void ClientQt::Stop() {
	thread_.exit();
}

void ClientQt::Wait() {
	thread_.wait();
}

void ClientQt::OnReadyRead() {
	socket_.write(socket_.readAll());
	socket_.flush();
	++count_;
}
